﻿using ProjectFootball.MVC.Areas.CountryArea.Models;
using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using ProjectFootball.MVC.Models.RepositoryProviders;
using System;
using System.Linq;
using System.Security.Principal;
using System.Web.Mvc;

namespace ProjectFootball.MVC.Areas.CountryArea.Controllers
{
    [Authorize]
    public class EditorController : BaseController
    {
        #region Constructor
        public EditorController(IRepositoryProvider provider)
            : base(provider)
        {
        }
        #endregion

        #region /CountryArea/Editor
        public ActionResult Index(Guid id)
        {
            var country = Provider.CountryRepository.SearchByPrimaryKey(id);

            if (country == null)
                return RedirectHome;

            return View(country.GetDataModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(CountryDataModel countryDataModel)
        {
            if (!ModelState.IsValid)
                return View(countryDataModel);

            var country = Provider.CountryRepository.SearchByPrimaryKey(countryDataModel.CountryGuid);

            if (country.CanEdit(User))
                country.Edit(countryDataModel, User);
            else
            {
                country = countryDataModel.CreateUnapproved(User);
                Provider.CountryRepository.Add(country);
            }

            Provider.SaveChanges();

            return RedirectToAction("Index", "Details", new { id = country.Pk });
        }
        #endregion

        #region /CountryArea/Editor/CreateNew
        public ActionResult CreateNew()
        {
            return View("Index", new CountryDataModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateNew(CountryDataModel countryDataModel)
        {
            if (!ModelState.IsValid)
                return View("Index", countryDataModel);

            var country = countryDataModel.CreateNew(User);

            Provider.CountryRepository.Add(country);

            Provider.SaveChanges();
            return RedirectToAction("Index", "Details", new { id = country.Pk });
        }
        #endregion

        #region /CountryArea/Editor/Delete
        public ActionResult Delete(Guid id)
        {
            var country = Provider.CountryRepository.SearchByPrimaryKey(id);

            if (country == null || !country.CanDelete(User))
                return RedirectHome;

            Provider.CountryRepository.Remove(country);
            Provider.SaveChanges();

            return RedirectHome;
        }
        #endregion

        #region /CountryArea/Editor/RecordForApproval
        [Authorize(Roles = "Admin, Superuser")]
        public ActionResult RecordForApproval(Guid id)
        {
            var country = Provider.CountryRepository.SearchByPrimaryKey(id);
            var countries = Provider.CountryRepository.Where(c => c.ApprovalGuid == country.ApprovalGuid);

            return View("Approval", countries.Search(User, SearchType.RecordForApproval));
        }
        #endregion

        #region /CountryArea/Editor/ConfirmApproval
        [Authorize(Roles = "Admin, Superuser")]
        public ActionResult ConfirmApproval(Guid id)
        {
            if (!User.IsApprover())
                return RedirectHome;

            var country = Provider.CountryRepository.SearchByPrimaryKey(id);

            if (country == null || !country.CanApprove(User))
                return RedirectHome;

            var countryQuery = from c in Provider.CountryRepository.Where(c => c.ApprovalGuid == country.ApprovalGuid && c.IsApproved)
                               select c;

            Guid approvedCountryGuid;

            if (countryQuery.Any())
            {
                var approvedCountry = countryQuery.Single();

                approvedCountryGuid = approvedCountry.Pk;
                country.Approve(approvedCountry);

                Provider.SaveChanges();
            }
            else
            {
                country.IsApproved = true;
                approvedCountryGuid = country.Pk;
                Provider.SaveChanges();
            }

            var countries = Provider.CountryRepository.Where(c => c.ApprovalGuid == country.ApprovalGuid && !c.IsApproved);
            Provider.CountryRepository.RemoveAll(countries);

            Provider.SaveChanges();

            return RedirectToAction("Index", "Details", new { id = approvedCountryGuid });
        }
        #endregion

        #region CountryArea/Editor/PendingApprovals
        [Authorize(Roles = "Admin, Superuser")]
        public ActionResult PendingApprovals()
        {
            var countries = Provider.CountryRepository.Where(c => c.IsApproved == false);

            return View("Approval", countries.Search(User, SearchType.PendingApprovals));
        }
        #endregion
    }
}
